<?php if (!defined('BASEPATH')) exit('No direct access allowed.');

/**
 * 扩展 核心钩子类库
 */
class MY_Hooks extends CI_Hooks {

    public function __construct() {
        parent::__construct();
        log_message('debug', "MY_Hooks Core Class Initialized");
    }

    /**
     * 注册钩子
     *
     * 一般在application/config/hooks.php注册钩子
     * 特殊情况:
     * 当核心钩子已加载后允许你注册钩子
     * <code>
     * $this->CI->hooks->register_hook('display_override', array(
     *     'class' => 'MyClass',
     *     'function' => 'hook_display',
     *     'filename' => 'MyClass.php',
     *     'filepath' => 'libraries',
     *     'params' => array('foo', 'bar', 'baz')
     * ));
     * </code>
     *
     * @param    string $hook 钩子名称(挂钩点)
     * @param    array $opts 钩子参数
     * @return    void
     */
    public function register_hook($hook, $opts) {
        if (!isset($this->hooks[$hook])) {
            $this->hooks[$hook] = array();
        }
        $this->hooks[$hook] = $opts;
    }

    /**
     * 调用钩子
     *
     * 调用一个特定的钩子(有原来的_call_hook()修改而来)
     * 增加第二个参数, 允许调钩子时传递参数给执行的方法
     *
     * @param string $which 钩子名称(挂钩点)
     * @param mixed $params 你希望传递给执行方法的任何参数
     * @return    mixed
     */
    public function call_hook($which = '', $params = null) {
        if (!$this->enabled or !isset($this->hooks[$which])) return false;

        if (isset($this->hooks[$which][0]) and is_array($this->hooks[$which][0])) {
            foreach ($this->hooks[$which] as $val) {
                //传入参数,替换默认的参数
                if (!empty($params)) $val['params'] = $params;
                $this->_run_hook($val);
            }
        } else {
            //传入参数,替换默认的参数
            if (!empty($params)) $this->hooks[$which]['params'] = $params;
            $this->_run_hook($this->hooks[$which]);
        }

        return true;
    }
}

/* End of file MY_Hooks.php */
/* Location: ./application/core/MY_Hooks.php */
